package com.aloneness.dingtalk.learn.service.impl;

import com.aloneness.dingtalk.learn.dao.ExpressDao;
import com.aloneness.dingtalk.learn.domain.Express;
import com.aloneness.dingtalk.learn.domain.ExpressDetail;
import com.aloneness.dingtalk.learn.service.IExpressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ExpressServiceImpl implements IExpressService {

    @Autowired
    private ExpressDao expressDao;

    @Override
    public List<Express> selectExpressList(String keyword) {
        return expressDao.selectExpressList(keyword);
    }

    @Override
    public List<Express> selectPublishExpressList(String publishId) {
        return expressDao.selectPublishExpressList(publishId);
    }

    @Override
    public List<Express> selectAcceptExpressList(String acceptId) {
        return expressDao.selectAcceptExpressList(acceptId);
    }

    @Override
    public Express selectExpressById(int expressId) {
        return expressDao.selectExpressById(expressId);
    }

    @Override
    public int updateExpress(Express express) {
        return expressDao.updateExpress(express);
    }

    @Override
    @Transactional
    public int insertExpress(Express express) {
        String expressDetailId = String.valueOf(System.currentTimeMillis());
        express.getExpressDetail().setExpressDetailId(expressDetailId);
        expressDao.insertExpressDetail(express.getExpressDetail());
        return expressDao.insertExpress(express);
    }

    @Override
    @Transactional
    public int finishExpress(Express express) {
        ExpressDetail expressDetail = new ExpressDetail();
        expressDetail.setExpressFinishTime(new Date());
        express.setExpressDetail(expressDetail);
        expressDao.updateFinishTime(express.getExpressDetail());
        express.setOrderStatus(1003);
        return expressDao.updateExpress(express);
    }

    @Override
    public int cancelExpress(Express express) {
        express.setOrderStatus(1004);
        return expressDao.updateExpress(express);
    }
}
